package com.changgou.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.item.service.PageService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhengjd
 * @date 2020/5/13 21:24
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class PageServiceImpl implements PageService {

    @Autowired
    TemplateEngine templateEngine;


    @Value("${pagepath}")
    private String pagePath;  //静态页面存放路径

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    /**
     * 根据spuId生成静态页面
     * @param id
     */
    @Override
    public void createHtml(Long id) {
        //1.上下文
        Context context = new Context();
        //2.准备文件
        Map<String, Object> dateMap = buildDataModel(id);
        context.setVariables(dateMap);
        //3.生成页面
        File dirFile = new File(pagePath);

        if (!dirFile.exists()){
            //文件夹不存在则创建
            dirFile.mkdirs();
        }
        //生成目标静态页面文件
        File destFile = new File(dirFile, id + ".html");
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(destFile, "UTF-8");
            templateEngine.process("myitem", context, writer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    /**
     * 根据spuId构建模板所需要的数据
     * @param id
     * @return
     */
    private Map<String, Object> buildDataModel(Long id) {
        Map<String, Object> dataMap = new HashMap<>();
        //根据spuId查询spu数据
        Result<Spu> spuResult = spuFeign.findById(id);
        Spu spu = spuResult.getData();
        //根据spu获取三级分类的Id
        Category categoryResult1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        Category categoryResult2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        Category categoryResult3 = categoryFeign.findById(spu.getCategory3Id()).getData();

        //根据spuId获取sku数据
        Sku condition = new Sku();
        condition.setSpuId(id);
        Result<List<Sku>> skuFeignList = skuFeign.findList(condition);

        //将获取到的数据存入到map中
        //spu中的图片资源
        if (spu.getImages()!=null){
            dataMap.put("imageList",spu.getImages().split(","));
        }
        //spu数据
        dataMap.put("specificationList", JSON.parseObject(spu.getSpecItems(),Map.class));
        dataMap.put("spu",spu);

        //sku列表数据
        dataMap.put("skuList",skuFeignList.getData());

        //分类数据
        dataMap.put("category1",categoryResult1);
        dataMap.put("category2",categoryResult2);
        dataMap.put("category3",categoryResult3);

        return dataMap;
    }
}
